公式:查代碼代入公式1+9x0+8x1+7x2+6x3+5x4+4x5+3x6+2x7+8+9
加總的數之後除以10整除(餘數為0)的話是身分證正確
完成後代碼
#代碼表
idcheck= [10,11,12,13,14,15,16,17,34,18,19,20,21,22,35,23,24,25,26,27,28,29,32,30,31,33]
TotalVal=0
idcode=""
while (len(idcode)!=10):
idcode=input("輸入身分證號:") #A123456789
print ("身分證:" + idcode)
CapVal = idcheck[ord(idcode[0])-65] #查表
idcode11 = str (CapVal) + idcode[1:10]
TotalVal = int(idcode11[0]) #10123456789
for index,item in enumerate(idcode11[1:10]):
m = int(item) * (9 - index)
TotalVal = TotalVal + m
TotalVal = TotalVal + int(idcode11[10])
print ("身分證是",end='')
if TotalVal % 10 == 0:
print ("正確的")
else:
print ("錯誤的")
執行結果:
輸入身分證號:Q255163478
身分證:Q255163478
身分證是正確的